package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.SuddenCycleHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.zsmart.core.log.ZSmartLogger;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wanghl386
 * @version 1.0
 * @date 2021/5/25 21:36
 */
@Service
@AttrHandler(SuddenCycleHandler.class)
public class SuddenCycleLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(SuddenCycleLinkHandler.class);

    /**
     * 突发周期
     */
    private static final String SUDDEN_CYCLE = "200001824";

    /**
     * 突发开始
     */
    private static final String SUDDEN_START = "210004306";

    /**
     * 突发结束
     */
    private static final String SUDDEN_END = "210004307";

    /**
     * 周
     */
    private static final String WEEK = "100004331";

    /**
     * 月
     */
    private static final String MONTH = "100002333";

    /**
     * 年
     */
    private static final String YEAR = "100002481";

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        try {
            // 突发周期
            KiteAttr suddenCycle = inst.getAttr(SUDDEN_CYCLE);
            // 突发开始时间
            KiteAttr suddenStart = inst.getAttr(SUDDEN_START);
            // 突发结束时间
            KiteAttr suddenEnd = inst.getAttr(SUDDEN_END);

            suddenStart.setValue(null);
            suddenEnd.setValue(null);
            suddenStart.setValueDesc(null);
            suddenEnd.setValueDesc(null);

            if (WEEK.equals(suddenCycle.getValue())) {
                // 周 1-7
                List<AttrValue> attrValueList = AttrValueCache.get(suddenStart.getAttrId());
                List<KiteAttrValue> kiteAttrValueList = KiteBeanUtils
                    .copyPropsForList(attrValueList, KiteAttrValue.class);
                List<KiteAttrValue> lists = kiteAttrValueList.stream().filter(kiteAttrValue -> kiteAttrValue.getAttrValueDesc().contains("星期")).collect(Collectors.toList());
                suddenStart.setAttrValueTypeId("98A");
                suddenEnd.setAttrValueTypeId("98A");
                suddenStart.setAttrValues(lists);
                suddenEnd.setAttrValues(lists);
            } else if (MONTH.equals(suddenCycle.getValue())) {
                // 月 1-31
                List<AttrValue> attrValueList = AttrValueCache.get(suddenStart.getAttrId());
                List<KiteAttrValue> kiteAttrValueList = KiteBeanUtils
                    .copyPropsForList(attrValueList, KiteAttrValue.class);
                List<KiteAttrValue> lists = kiteAttrValueList.stream().filter(kiteAttrValue -> kiteAttrValue.getAttrValueDesc().contains("每月")).collect(Collectors.toList());
                suddenStart.setAttrValueTypeId("98A");
                suddenEnd.setAttrValueTypeId("98A");
                suddenStart.setAttrValues(lists);
                suddenEnd.setAttrValues(lists);
            } else {
                // 年 0101 -1231
                // 月 1-31
                // 周 1-7
                suddenStart.setAttrValueTypeId("98Q");
                suddenEnd.setAttrValueTypeId("98Q");
                suddenStart.setAttrValues(null);
                suddenEnd.setAttrValues(null);
            }
        } catch (Exception e) {
            LOGGER.error("handle-->", e, e.getMessage());
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
